package com.example.demo.DAO.myBatisDao.impl;

import com.example.demo.DAO.myBatisDao.UserDao;
import com.example.demo.pojo.mysqlPojo.myUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements UserDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int add(myUser myuser) {
        return jdbcTemplate.update("insert into myUser values(?,?,?,date?)",null,myuser.getUsername(),myuser.getPassword(),myuser.getBirthday());
    }

    @Override
    public int update(myUser myuser) {
        return jdbcTemplate.update("update myUser set username = ? ,password = ? where id = ?",myuser.getUsername(),myuser.getPassword());
    }

    @Override
    public int delete(int id) {
        return jdbcTemplate.update("delete from myUser where id = ?",id);
    }

    @Override
    public myUser findById(int id) {
       List<myUser> list= jdbcTemplate.query("select * from myUser where id=?",
               new Object[]{id},new BeanPropertyRowMapper(myUser.class));
        if (list!=null&&list.size()>0){

            myUser myuser=list.get(0);
            return myuser;
        }else{
            return null;
        }
    }

    @Override
    public List<myUser> findUserList() {
        List<myUser> list= jdbcTemplate.query("select * from myUser where id=?",
                new Object[]{},new BeanPropertyRowMapper(myUser.class));
        if (list!=null&&list.size()>0){
            return list;
        }else{
            return null;
        }
    }
}
